package com.Servlet;

import com.DAO.GradeDao;
import com.Grade;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/gradeManagement")
public class GradeServlet extends HttpServlet {
    private GradeDao gradeDao;

    @Override
    public void init() throws ServletException {
        gradeDao = new GradeDao();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");

        // 处理删除操作
        if ("delete".equals(action)) {
            int id = Integer.parseInt(request.getParameter("id"));
            try {
                gradeDao.deleteGrade(id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            response.sendRedirect("gradeManagement");
        }
        // 处理编辑操作
        else if ("edit".equals(action)) {
            int id = Integer.parseInt(request.getParameter("id"));
            Grade grade = null;
            try {
                grade = gradeDao.getGradeById(id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (grade != null) {
                // 设置请求属性，让在编辑页面显示数据
                request.setAttribute("id", grade.getId());
                request.setAttribute("studentId", grade.getStudentId());
                request.setAttribute("courseName", grade.getCourseName());
                request.setAttribute("grade", grade.getGrade());
                request.getRequestDispatcher("editGrade.jsp").forward(request, response);
            }
        }
        // 获取所有成绩并展示
        else {
            List<Grade> grades = null;
            try {
                grades = gradeDao.getAllGrades();
                System.out.println("查询到的数量: " + grades.size()); // 打印查询到的成绩数量
            } catch (SQLException e) {
                e.printStackTrace();
            }
            request.setAttribute("grades", grades);
            request.getRequestDispatcher("gradeManagement.jsp").forward(request, response);
        }
    }

    // 处理POST请求（主要是用于更新成绩等操作）
    /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        // 更新成绩
        if ("update".equals(action)) {
            int id = Integer.parseInt(request.getParameter("id"));
            int studentId = Integer.parseInt(request.getParameter("studentId"));
            String courseName = request.getParameter("courseName");
            double gradeValue = Double.parseDouble(request.getParameter("grade"));

            Grade grade = new Grade();
            grade.setId(id);
            grade.setStudentId(studentId);
            grade.setCourseName(courseName);
            grade.setGrade(String.valueOf(gradeValue));

            try {
                gradeDao.updateGrade(grade);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            response.sendRedirect("gradeManagement");
        }
    }*/
}
